//
//  ActivityViewController.m
//  Copy_AiKapPu_Personal_OC
//
//  Created by taojianz on 2017/7/11.
//  Copyright © 2017年 taojianz. All rights reserved.
//

#import "ActivityViewController.h"
#import "AKPActivityTableViewCell.h"
#import "MJRefresh.h"

@interface ActivityViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *socialCategoryBtn;
@property (weak, nonatomic) IBOutlet UIButton *workspaceCategoryBtn;
@property (weak, nonatomic) IBOutlet UIButton *wishTreeBtn;

@end

@implementation ActivityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    NSArray *arr = @[@{@"bgImageStr":@"jiehun-hui",@"status":@"活动状态-进行中",@"location":@"宁波优跨科技技术有限公司",
                       @"time":@"2017-7-5",@"synopsis":@"老大的幸福－宁波大龄专场交友派对",@"price":@"免费"},
                     @{@"bgImageStr":@"jiehun-hui",@"status":@"活动状态-进行中",@"location":@"宁波优跨科技技术有限公司",
                       @"time":@"2017-7-5",@"synopsis":@"老大的幸福－宁波大龄专场交友派对",@"price":@"免费"},
                     @{@"bgImageStr":@"jiehun-hui",@"status":@"活动状态-进行中",@"location":@"宁波优跨科技技术有限公司",
                       @"time":@"2017-7-5",@"synopsis":@"老大的幸福－宁波大龄专场交友派对",@"price":@"免费"},
                     @{@"bgImageStr":@"jiehun-hui",@"status":@"活动状态-进行中",@"location":@"宁波优跨科技技术有限公司",
                       @"time":@"2017-7-5",@"synopsis":@"老大的幸福－宁波大龄专场交友派对",@"price":@"免费"},
                     @{@"bgImageStr":@"jiehun-hui",@"status":@"活动状态-进行中",@"location":@"宁波优跨科技技术有限公司",
                       @"time":@"2017-7-5",@"synopsis":@"老大的幸福－宁波大龄专场交友派对",@"price":@"免费"},
                     @{@"bgImageStr":@"jiehun-hui",@"status":@"活动状态-进行中",@"location":@"宁波优跨科技技术有限公司",
                       @"time":@"2017-7-5",@"synopsis":@"老大的幸福－宁波大龄专场交友派对",@"price":@"免费"},
                       ];
    self.infoArray = [[NSMutableArray alloc]initWithArray:arr];
    [self setupMJRecresh];
    [self.socialCategoryBtn setSelected:true];
}

// 设置刷新控件
-(void)setupMJRecresh{
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadActifityViewDataByDropDown)];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadActifityViewData)];
    [self.tableView.mj_header beginRefreshing];
}
// FIXME: 实现真正的排序
- (IBAction)CategoryBtnTapped:(UIButton *)sender {
    switch (sender.tag) {
        case 100:// 社交类
            [sender setSelected:true];
            [self.wishTreeBtn setSelected:false];
            [self.workspaceCategoryBtn setSelected:false];
            break;
        case 101:// 职场类
            [sender setSelected:true];
            [self.socialCategoryBtn setSelected:false];
            [self.wishTreeBtn setSelected:false];
            break;
        case 102:// 愿望树
            [sender setSelected:true];
            [self.socialCategoryBtn setSelected:false];
            [self.workspaceCategoryBtn setSelected:false];
            break;
        default:
            break;
    }
    [self.tableView.mj_header beginRefreshing];
    
}

// 模拟下拉刷新数据
-(void)loadActifityViewData{
    NSInteger count = [self.tableView numberOfSections];
    
    NSString *str_C = [[NSString alloc] initWithFormat:@"下拉刷新%zd", count+1] ;
    
    NSDictionary *newUser = @{@"bgImageStr":@"jiehun-hui",@"status":@"活动状态-进行中",@"location":@"宁波优跨科技技术有限公司",
                              @"time":@"2017-7-5",@"synopsis":str_C,@"price":@"免费"};
    [_infoArray insertObject:newUser atIndex:0];
    
    [self.tableView reloadData];
    
    [self performSelector:@selector(endRefreshing) withObject:nil afterDelay:2];
    
    
    NSLog(@"现有数据条数:%zd",[self.tableView numberOfSections]);
}

// 模拟上拉刷新数据
-(void)loadActifityViewDataByDropDown{
    NSInteger count = [self.tableView numberOfSections];
    
    NSString *str_C = [[NSString alloc] initWithFormat:@"上拉刷新%zd", count+1] ;
    
    NSDictionary *newUser = @{@"bgImageStr":@"jiehun-hui",@"status":@"活动状态-进行中",@"location":@"宁波优跨科技技术有限公司",
                              @"time":@"2017-7-5",@"synopsis":str_C,@"price":@"免费"};
    [_infoArray addObject:newUser];
    [self.tableView reloadData];
    // FIXME: 延迟两秒刷新数据，在这两秒内由于还么有结束footer的刷新状态，所以无法完成下一次刷新
    [self performSelector:@selector(endRefreshing) withObject:nil afterDelay:2];
    
    
    NSLog(@"现有数据条数:%zd",[self.tableView numberOfSections]);
}
// 刷新控件停止动画
-(void)endRefreshing{
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AKPActivityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AKPActivityTableViewCell" forIndexPath:indexPath];
    
    [cell setCellContentWithDict:_infoArray[indexPath.section]];
    
    return cell;
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.infoArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}

@end
